VB:Tutorials:Windows Resource Files
Resource files enable you to store bitmaps, waves, AVI's, icons, cursors, strings, and more... all in one file. It is to your advantage to use a resource file, the alternative is to leave all of your resources exposed, where the user can get at them and modify them. Once you've squished them into a resource file the user cannot touch them! HA! So, to make a resource file you need a couple of things from your VB CD-ROM installation disk. Go root around in there and find the \Tools subdirectory of the main Visual Basic directory. Found it? Good. That RC.EXE file is what you need (have a look at the help files too). Copy it to your hard drive somewhere; it's just easier that way. Now, you need to make a file called FILENAME.RC in a text editor. Change FILENAME to whatever you want the name of your resource file to be. Inside the text editor, you will describe which files you want to include in the resource, and how you want to refer to them in code. The syntax is: nameID keyword filename ; nameID : the name by which you will refer to this item in code. ; keyword : a keyword defining what type of file this is. It can be BITMAP, CURSOR, ICON, SOUND, or VIDEO. SOUND is a .WAV file, VIDEO is an .AVI file. ; filename : the path to the file on your hard drive. Ok, for example, if I want to load a bitmap called VIEWSCRN.BMP into my resource file, and refer to that bitmap as ViewScreen in my code, I'd write: ViewScreen BITMAP C:\BITMAPS\VIEWSCRN.BMP We keep adding entries to the .RC file until we've included everything we wish to include. It'll end up looking something like this: ViewScreen BITMAP C:\BITMAPS\VIEWSCRN.BMP SpaceShip BITMAP C:\BITMAPS\SPACESHIP.BMP Explosion SOUND C:\SOUNDS\EXPLOSION.WAV CutScene VIDEO C:\AVIS\CUTSCENE.AVI Once you're done, save the file and run the RC.EXE file on it. This program will gather all of the files you've listed and compile them into a file called FILENAME.RES. Use the command line (yes, I said command line... venture out into the world of DOS for a moment) RC /r FILENAME.RC ... and it'll spit out the resource file for you. Now, go into your VB Project and add the resource file by pressing Ctrl-D and locating the file. Once you've included the resource in your project you can start using it in code. Use LoadResData or LoadResPicture to use the data you've stored in the resource file. LoadResPicture("ViewScreen",0) This command will return the bitmap VIEWSCRN.BMP that we stored in the resource file. We can then place it in a picture box control, or on a DirectDraw surface. The second argument for the LoadResPicture function determines what type of file we're accessing, 0 refers to bitmaps. 1 refers to icons, and 2 to cursors. Other types (Such as AVI's and WAV's) must be accessed through the LoadResData function. For sample source code demonstrating how to use resource files with DirectX click here. Category:VB Category:Tutorial